import { Injectable } from '@angular/core';
@Injectable({
  providedIn: 'root'
})
export class DateService {
  constructor() { }
  public formatDate(timestamp: string, type: number): string {
    var time = new Date(parseInt(timestamp));
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    if (type == 1) {
      return y + '.' + this.add0(m) + '.' + this.add0(d) + ' ' + this.add0(h) + ':' + this.add0(mm) + ':' + this.add0(s);
    }
    if (type == 2) {
      return y + '-' + this.add0(m) + '-' + this.add0(d);
    }
    if (type == 3) {
      return this.add0(h) + ':' + this.add0(mm);
    }
    if (type == 4) {
      return this.add0(h) + ':' + this.add0(mm)+':' + this.add0(s);
    }
    if (type == 5) {
      return m + '月' + d + '日 ' + this.add0(h) + ':' + this.add0(mm) + ':' + this.add0(s);
    }
    if (type == 6) {
      return y + '.' + m + '.' + d + ' ' + this.add0(h) + ':' + this.add0(mm) + ':' + this.add0(s);
    }

  }
  public add0(m: number): string {
    return m < 10 ? '0' + m : m + "";
  }
  public getNowDate(type: number) {
    var myDate = new Date();
    var y = myDate.getFullYear();
    var m = myDate.getMonth() + 1;
    var d = myDate.getDate();
    var h = myDate.getHours();
    var mm = myDate.getMinutes();
    var s = myDate.getSeconds();
    if (type == 1) {
      return y + '-' + this.add0(m) + '-' + this.add0(d) + ' ' + this.add0(h) + ':' + this.add0(mm) + ':' + this.add0(s);
    }
    if (type == 2) {
      return y + '-' + this.add0(m) + '-' + this.add0(d);
    }
    if (type == 3) {
      return this.add0(h) + ':' + this.add0(mm);
    }
  }
  public activityStart(startDate, endDate, setStart) {
    var myDate = new Date();
    var myDay = myDate.getDate() < 10 ? '0' + myDate.getDate() : myDate.getDate();
    var myMonth = (myDate.getMonth() + 1) < 10 ? '0' + (myDate.getMonth() + 1) : (myDate.getMonth() + 1);
    var today = myDate.getFullYear() + '-' + myMonth + '-' + myDay;
    if (today >= startDate && today <= endDate || today > endDate) {
      return setStart = true;
    } else {
      return setStart = false;
    }
  }
  public activiStart(startDate, endDate) {
    var myDate = new Date();
    var myDay = myDate.getDate() < 10 ? '0' + myDate.getDate() : myDate.getDate();
    var myMonth = (myDate.getMonth() + 1) < 10 ? '0' + (myDate.getMonth() + 1) : (myDate.getMonth() + 1);
    var today = myDate.getFullYear() + '-' + myMonth + '-' + myDay;
    if (today >= startDate && today <= endDate || today > endDate) {
      return true;
    } else {
      return false;
    }

  }
}
